/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.risk.hid.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.risk.hid.entity.RiskHidCheckTaskRecordEntity;
import com.hopes.bizz.risk.hid.service.RiskHidCheckTaskRecordService;
import com.hopes.bizz.risk.hid.vo.RiskHidCheckTaskHidVO;
import com.hopes.bizz.risk.hid.vo.RiskHidCheckTaskRecordVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 隐患检查记录
 *
 * @author daijie
 * @date 2023-07-28 15:20:59
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/riskHidCheckTaskRecord" )
@Tag(description = "riskHidCheckTaskRecord" , name = "隐患检查记录管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskHidCheckTaskRecordController {

    private final  RiskHidCheckTaskRecordService riskHidCheckTaskRecordService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param riskHidCheckTaskRecord 隐患检查记录
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('risk_hid_check_task_record_view')" )
    public R getriskHidCheckTaskRecordPage(Page page, RiskHidCheckTaskRecordEntity riskHidCheckTaskRecord) {
        LambdaQueryWrapper<RiskHidCheckTaskRecordEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(riskHidCheckTaskRecordService.page(page, wrapper));
    }


    /**
     * 通过id查询隐患检查记录
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('risk_hid_check_task_record_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(riskHidCheckTaskRecordService.getById(id));
    }

    /**
     * @Description 提交隐患
     * @Author lyb
     * @Date 2023/6/16
     **/
    @GetMapping(value = "initHidInfo/{id}")
    @PreAuthorize("@pms.hasPermission('risk_hid_check_task_record_view')" )
    public R initHidInfo(@PathVariable Long id) {
        RiskHidCheckTaskHidVO riskHidCheckTaskHidVo = riskHidCheckTaskRecordService.initHidInfo(id);
        return R.ok(riskHidCheckTaskHidVo);
    }

    @Operation(summary = "根据任务id查询检查任务项" , description = "根据任务id查询检查任务项" )
    @GetMapping(value = "listByTaskId/{taskId}")
    @PreAuthorize("@pms.hasPermission('risk_hid_check_task_record_view')" )
    public R listByTaskId(@PathVariable Long taskId) {
        List<RiskHidCheckTaskRecordEntity> list = riskHidCheckTaskRecordService.listByTaskId(taskId);
        List<RiskHidCheckTaskRecordVO> voList = new ArrayList<>();
        for (RiskHidCheckTaskRecordEntity record : list) {
            RiskHidCheckTaskRecordVO vo = new RiskHidCheckTaskRecordVO();
            BeanUtils.copyProperties(record, vo);
            voList.add(vo);
        }
        return R.ok(voList);
    }

    /**
     * @Description 查询异常项
     * @Author lyb
     * @Date 2023/6/16
     **/
    @GetMapping(value = "listExByTaskId/{taskId}")
    @PreAuthorize("@pms.hasPermission('risk_hid_check_task_record_view')" )
    public R listExByTaskId(@PathVariable Long taskId) {
        List<RiskHidCheckTaskRecordEntity> list = riskHidCheckTaskRecordService.listExByTaskId(taskId);
        return R.ok(list);
    }

    @Operation(summary = "根据任务id查询检查任务类型" , description = "根据任务id查询检查任务类型" )
    @GetMapping(value = "getCheckType/{id}")
    @PreAuthorize("@pms.hasPermission('risk_hid_check_task_record_view')" )
    public R getCheckType(@PathVariable Long id) {
        Integer checkType = riskHidCheckTaskRecordService.getCheckType(id);
        return R.ok(checkType);
    }

    /**
     * 新增隐患检查记录
     * @param riskHidCheckTaskRecord 隐患检查记录
     * @return R
     */
    @Operation(summary = "新增隐患检查记录" , description = "新增隐患检查记录" )
    @SysLog("新增隐患检查记录" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('risk_hid_check_task_record_add')" )
    public R save(@RequestBody RiskHidCheckTaskRecordEntity riskHidCheckTaskRecord) {
        return R.ok(riskHidCheckTaskRecordService.save(riskHidCheckTaskRecord));
    }

    /**
     * 修改隐患检查记录
     * @param riskHidCheckTaskRecord 隐患检查记录
     * @return R
     */
    @Operation(summary = "修改隐患检查记录" , description = "修改隐患检查记录" )
    @SysLog("修改隐患检查记录" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('risk_hid_check_task_record_edit')" )
    public R updateById(@RequestBody RiskHidCheckTaskRecordEntity riskHidCheckTaskRecord) {
        return R.ok(riskHidCheckTaskRecordService.updateById(riskHidCheckTaskRecord));
    }

    /**
     * 通过id删除隐患检查记录
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除隐患检查记录" , description = "通过id删除隐患检查记录" )
    @SysLog("通过id删除隐患检查记录" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('risk_hid_check_task_record_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(riskHidCheckTaskRecordService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param riskHidCheckTaskRecord 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('hid_record_export')" )
    public List<RiskHidCheckTaskRecordEntity> export(RiskHidCheckTaskRecordEntity riskHidCheckTaskRecord) {
        return riskHidCheckTaskRecordService.list(Wrappers.query(riskHidCheckTaskRecord));
    }
}